package com.example.service.impl;
import com.example.mapper.ContactsMapper;
import com.example.service.ICompanyService;
import com.example.service.IContactsGroupDetailService;
import com.example.service.IContactsGroupService;
import com.example.util.EasyExcelGenerator;
import com.example.vo.DictVo;
import com.example.vo.excel.ContactsExcelVo;
import com.example.vo.respvo.ContactsVo;
import com.example.service.IContactsService;
import com.example.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通讯录服务实现类
 *
 * @author wsl
 */
@Service
public class ContactsServiceImpl implements IContactsService {

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private IContactsGroupService contactsGroupService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IContactsGroupDetailService contactsGroupDetailService;

    @Override
    public ResultVo getOrganizationContacts() {
        ResultVo organization = companyService.getCompanyWithDeptList();
        List<ContactsVo> organizationContacts = contactsMapper.selectOrganizationContacts();
        Map<String, Object> map = new HashMap<>();
        map.put("organization",organization.getData());
        map.put("organizationContacts",organizationContacts);
        return ResultVo.response(map);
    }

    @Override
    public ResultVo getUserGroupContacts(Integer empId) {
        Map<String, Object> map = new HashMap<>();
        ResultVo userGroupContacts = contactsGroupDetailService.getContactsGroupDetailPageInfo(empId);
        ResultVo userContactsGroup = contactsGroupService.getUserContactsGroup(empId);
        map.put("userGroupContacts",userGroupContacts.getData());
        map.put("userContactsGroup",userContactsGroup.getData());
        return ResultVo.response(map);
    }

    @Override
    public void exportOrganizationContacts(HttpServletResponse response) {
        //获取用户通讯录信息
        List<ContactsVo> organizationContacts = contactsMapper.selectOrganizationContacts();
        //导出用户通讯录数据集合
        List<ContactsExcelVo> exportData = new ArrayList<>();
        organizationContacts.forEach(contactsVo->{
            ContactsExcelVo contactsExcelVo = new ContactsExcelVo();
            BeanUtils.copyProperties(contactsVo,contactsExcelVo);
            exportData.add(contactsExcelVo);
        });
        EasyExcelGenerator.write(response,"员工通讯录", ContactsExcelVo.class,"员工通讯录",exportData);
    }
}
